package com.jerry.huihome.module.find_near_mine.fragment;

import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find_near_mine.adapter.ActivityListAdapter;
import com.jerry.huihome.module.find_near_mine.entity.ActivityListEntity;
import com.jerry.huihome.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by axiang on 2016/5/10.
 */
public class Find_ActiviytFragment extends BaseFragment{

    private List<ActivityListEntity.BodyBean.ItemsBean> itemsBeanList;
    @BindView(R.id.activity_list)
    PullToRefreshListView activity_list;
    private ActivityListAdapter activityAdapter;

    public Find_ActiviytFragment(){
    }

    @Override
    protected int getLayoutId(){

        return R.layout.pulltorefreshview_activity;
    }


    @Override
    protected void initViews(){
        itemsBeanList = new ArrayList<>();
        activityAdapter = new ActivityListAdapter(getActivity(),itemsBeanList);
        activity_list.setAdapter(activityAdapter);
        activity_list.getRefreshableView().setDivider(null);
        activity_list.getRefreshableView().setSelector(android.R.color.transparent);
        activity_list.setMode(PullToRefreshBase.Mode.BOTH);
        activity_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>(){
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView){
                mPagenum =1;
                loadData();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView){
                mPagenum++;
                loadData();
            }
        });

    }
    private int mPagenum = 1;
    private String reqUrl = Urls.ACTIVITY_URL + "pageIndex=" +mPagenum;

    @Override
    protected void loadData(){
        OkHttpUtils.get(reqUrl, new OkHttpUtils.OnGetDataListener(){
            @Override
            public void onResponse(String url, String data){
                if(mPagenum==1){
                    itemsBeanList.clear();

                }
                Gson gson = new Gson();
                ActivityListEntity activityListEntity = gson.fromJson(data, ActivityListEntity.class);
                if(activityListEntity!=null
                        &&activityListEntity.getBody().getItems()!=null
                        &&!activityListEntity.getBody().getItems().isEmpty()){
                    itemsBeanList.addAll(activityListEntity.getBody().getItems());
                    activityAdapter.notifyDataSetChanged();
                    activity_list.onRefreshComplete();
                }
            }

            @Override
            public void onFailure(String url, String error){
            }
        });

    }
}
